import mysql2 from 'mysql2'
import { PoolConnection } from 'mysql2'
import { dbConfig } from '../config/db.config.js'
/**
 * @Author HuiLY
 * @Date 2023-03-08 16:28:15
 * 创建数据库连接
 */
const MysqlPool = mysql2.createPool(dbConfig)

/**
 * @returns {Promise<PoolConnection>}
 */
function GetConnection() {
    return new Promise((resolve, reject) => {
        MysqlPool.getConnection((err, connection) => {
            if (err) reject(err)
            resolve(connection);
        })
    })
}
/**
 * @param {String} sql
 * @param {Array} condition
 * @param {PoolConnection} conn
 * @param {boolean} state 是否开启事务 默认false
 * @returns {Promise<{rows:mysql2.RowDataPacket[] | mysql2.RowDataPacket[][] | mysql2.OkPacket | mysql2.OkPacket[] | mysql2.ResultSetHeader,fields:mysql2.FieldPacket[]}>}
 * @example resolve({ rows, fields })
 */
function GetResult(sql, condition, conn, state = false) {
    return new Promise((resolve, reject) => {
        if (!conn) reject('没有连接')
        if (state) {
            conn.beginTransaction((err) => {
                if (err) {
                    conn.release()
                    reject(err)
                } else {
                    // 成功开启事务
                    conn.query(sql, condition, (err, rows, fields) => {
                        if (err) {
                            conn.rollback((err) => {
                                if (err) console.error('事务回滚失败');
                            })
                            conn.release()
                            reject(err)
                        }
                        // 执行sql成功
                        conn.commit((err) => {
                            if (err) {
                                conn.rollback((err) => {
                                    if (err) console.error('事务回滚失败');
                                    conn.release()
                                    reject('事务提交失败!')
                                })
                            } else {
                                conn.release()
                                resolve({ rows, fields })
                            }
                        })
                    })
                }
            })
        } else {
            conn.query(sql, condition, (err, rows, fields) => {
                if (err) {
                    conn.release()
                    reject(err)
                } else {
                    conn.release()
                    resolve({ rows, fields });
                }
            })
        }
    })
}
/**
 * @param {String} sql
 * @param {Array} condition
 * @param {PoolConnection} conn
 * @returns {Object}
 * @example resolve({ rows, fields })
 */
function GetResult_Exexute(sql, condition, conn) {
    return new Promise((resolve, reject) => {
        if (!conn) reject('没有连接')
        conn.execute(sql, condition, (err, rows, fields) => {
            if (err) reject(err);
            resolve({ rows, fields });
        })
    })
}
export default MysqlPool;
export { GetConnection, GetResult };